package dmt16.TelemetrySuit.Controller.SensorTypes
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	public class PulseSensor implements ISensorType
	{
		public function PulseSensor()
		{
			_pulseData=new Vector.<Number>();
			_rawData=new Vector.<Number>();
			_rawData.length=WINDOW_SIZE;
			_rawData.fixed=true;
			_time=new Vector.<Number>();
		}
		
		public function get sensorType():int
		{
			return SensorType.PULSE;
		}
		
		public function get dataFormat():int
		{
			return DataFormat.UInt16;
		}
		
		private var _lastMeasure:Number=0;
		private var _n:Number=0;
		private var _i:Number=0;
		private var _lastPulseTime:Number=0;
		private var _movingAverage:Number=0;
		
		private static const WINDOW_SIZE:Number=40;
		
		public function addData(value:*, time:Number):void
		{
			var val:Number=value as Number;
			_rawData[_i%WINDOW_SIZE]=val;
			if(_i<WINDOW_SIZE){
				_n+=val;
			}
			else{
				_n+=_rawData[_i%WINDOW_SIZE]-_rawData[(_i-1)%WINDOW_SIZE];
				_movingAverage=_n/WINDOW_SIZE;
				if((val>(_movingAverage+10))&&(_lastMeasure<(_movingAverage+10))){
					if(_lastPulseTime>0){
						var p:Number=60000/(time-_lastPulseTime);
						if(p<250&&p>50){
							if(_pulse==0)
								_pulse=p;
							else
								_pulse=(p*2+_pulse)/3;
							_eventManager.dispatchEvent(new SensorEvent(SensorEvent.PULSE));
						}	
					}
					_lastPulseTime=time;
				}
			}
			_i++;
			_pulseData.push(_pulse);
			_lastMeasure=val;
			_eventManager.dispatchEvent(new SensorEvent(SensorEvent.SENSOR_DATA_EVENT));
		}
		
		public function pulseTrace():Vector.<Number>{
			var result:Vector.<Number>=new Vector.<Number>();
			for(var n:Number=_i;n<_i+WINDOW_SIZE;n++){
				result.push(_rawData[n%WINDOW_SIZE]);
			}
			return result;
		}
		
		private var _rawData:Vector.<Number>;
		
		private var _pulseData:Vector.<Number>;
		public function get pulseData():Vector.<Number>{
			return _pulseData;	
		}
		
		private var _pulse:Number=0;
		public function get pulse():Number{
			return _pulse;
		}
		
		private var _time:Vector.<Number>;
		public function get time():Vector.<Number>{
			return _time;	
		}
		
		private var _eventManager:EventDispatcher=new EventDispatcher();
		public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void
		{
			_eventManager.addEventListener(type,listener,useCapture,priority,useWeakReference);
		}
		public function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void{
			_eventManager.removeEventListener(type,listener,useCapture);
		}
		public function dispatchEvent(event:Event):Boolean{
			return _eventManager.dispatchEvent(event);
		}
		public function hasEventListener(type:String):Boolean{
			return _eventManager.hasEventListener(type);
		}
		public function willTrigger(type:String):Boolean{
			return _eventManager.willTrigger(type);
		}
	}
}